DESTDIR = $(shell pwd)
BUILDDIR = $(DESTDIR)/bundle
LUAJITDIR = linux/luajit-2.1
LUAJIT_INC_DIR = $(LUAJITDIR)/include/luajit-2.1
LUAJIT_LIB_DIR = $(LUAJITDIR)/lib
NGINXNAME = nginx-1.11.2
NGINXDIR = $(DESTDIR)/linux/$(NGINXNAME)
SOURCEDIR = $(DESTDIR)/build

EXT_INCLUDES = -I$(BUILDDIR)/ngx_lua-0.10.10/src -I$(BUILDDIR)/nginx-1.11.2/objs
EXT_INCLUDES += -I$(BUILDDIR)/nginx-1.11.2/src/core -I$(BUILDDIR)/nginx-1.11.2/src/event
EXT_INCLUDES += -I$(BUILDDIR)/nginx-1.11.2/src/event/modules -I$(BUILDDIR)/nginx-1.11.2/src/http
EXT_INCLUDES += -I$(BUILDDIR)/nginx-1.11.2/src/http/modules -I$(BUILDDIR)/nginx-1.11.2/src/http/v2
EXT_INCLUDES += -I$(BUILDDIR)/nginx-1.11.2/src/stream -I$(BUILDDIR)/nginx-1.11.2/src/os/unix


.PHONY: all install clean

all: builddir
	cd $(BUILDDIR)/LuaJIT-2.1-20170808 && $(MAKE) TARGET_STRIP=@: CCDEBUG=-g XCFLAGS='-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT' CC='gcc' PREFIX=$(LUAJITDIR)
	cd $(BUILDDIR)/LuaJIT-2.1-20170808 && $(MAKE) install TARGET_STRIP=@: CCDEBUG=-g XCFLAGS='-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT' CC='gcc' PREFIX=$(LUAJITDIR) DESTDIR=$(DESTDIR)/
	cd $(BUILDDIR)/lua-cjson-2.1.0.5 && $(MAKE) DESTDIR=$(DESTDIR) LUA_INCLUDE_DIR=$(DESTDIR)/$(LUAJIT_INC_DIR) LUA_CMODULE_DIR=$(LUAJIT_LIB_DIR) LUA_MODULE_DIR=$(LUAJIT_LIB_DIR) CJSON_CFLAGS="-g -fpic" CC='gcc'
	cd $(BUILDDIR)/lua-cbson-master && cmake . && make -j$(nproc)
	cd $(BUILDDIR)/lua-redis-parser-0.13 && $(MAKE) DESTDIR=$(DESTDIR) LUA_INCLUDE_DIR=$(DESTDIR)/$(LUAJIT_INC_DIR) LUA_LIB_DIR=$(LUAJIT_LIB_DIR) CC='gcc' EXT_INCLUDES="$(EXT_INCLUDES)"
	cd $(BUILDDIR)/lua-rds-parser-0.06 && $(MAKE) DESTDIR=$(DESTDIR) LUA_INCLUDE_DIR=$(DESTDIR)/$(LUAJIT_INC_DIR) LUA_LIB_DIR=$(LUAJIT_LIB_DIR) CC='gcc' EXT_INCLUDES="$(EXT_INCLUDES)"
	cd $(BUILDDIR)/lua-socket-2.0.2/src && $(MAKE) CC='gcc' LUAINC=-I$(DESTDIR)/$(LUAJIT_INC_DIR)
	cd $(BUILDDIR)/nginx-1.11.2 && $(MAKE) DESTDIR=$(DESTDIR) EXT_INCLUDES="$(EXT_INCLUDES)"

builddir:
	mkdir -p $(LUAJITDIR)

install: all
	rm -rf $(NGINXDIR)/*
	mkdir -p $(NGINXDIR)/logs
	cp -fr $(SOURCEDIR)/conf $(NGINXDIR)
	cp -fr $(SOURCEDIR)/html $(NGINXDIR)
	cp -fr $(SOURCEDIR)/lua $(NGINXDIR)
	cp -fr $(SOURCEDIR)/lualib $(NGINXDIR)
	cp -fr $(SOURCEDIR)/compilelua.sh $(NGINXDIR)
	mkdir -p $(NGINXDIR)/lualib/redis
	mkdir -p $(NGINXDIR)/lualib/rds
	
	cd $(BUILDDIR)/lua-cjson-2.1.0.5 && cp -f *.so $(NGINXDIR)/lualib
	cd $(BUILDDIR)/lua-cbson-master && cp -f *.so $(NGINXDIR)/lualib
	cd $(BUILDDIR)/lua-socket-2.0.2/src && cp -f *.so* $(NGINXDIR)/lualib
	cd $(BUILDDIR)/lua-redis-parser-0.13 && cp -f *.so $(NGINXDIR)/lualib/redis
	cd $(BUILDDIR)/lua-rds-parser-0.06 && cp -f *.so $(NGINXDIR)/lualib/rds
	cd $(BUILDDIR)/nginx-1.11.2/objs && cp -f nginx* $(NGINXDIR)
	
	cp -f $(LUAJITDIR)/bin/luajit $(NGINXDIR)
	cp -f $(LUAJITDIR)/lib/*.so.2 $(NGINXDIR)
	cp -f /usr/local/lib/libdrizzle.so.1 $(NGINXDIR)
	
	chmod +x $(NGINXDIR)/*.so* $(NGINXDIR)/nginx $(NGINXDIR)/luajit
	
	cd $(NGINXDIR) && sh compilelua.sh
	
	rm -f $(NGINXDIR)/compilelua.sh
	rm -f $(NGINXDIR)/conf/*.txt
	rm -fr $(NGINXDIR)/lua
	rm -f $(DESTDIR)/linux/*.tar.gz
	
	cd $(DESTDIR)/linux && tar czf $(NGINXNAME).tar.gz nginx*

clean:
	rm -rf $(NGINXDIR)
	cd $(BUILDDIR)/LuaJIT-2.1-20170808 && $(MAKE) clean
	cd $(BUILDDIR)/lua-socket-2.0.2/src && $(MAKE) clean
	cd $(BUILDDIR)/nginx-1.11.2 && $(MAKE) clean